
package com.jjwxc.jwjskandriod.readActivity.ui;

import com.jjwxc.jwjskandriod.readActivity.bean.BookChapterBean;
import com.jjwxc.jwjskandriod.readActivity.bean.ChapterInfoBean;
import com.jjwxc.jwjskandriod.readActivity.bean.CollBookBean;
import com.jjwxc.jwjskandriod.readActivity.model.ContentSoDuModelImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import io.reactivex.Single;
import io.reactivex.functions.Function;

/**
 * 图书内容获取 加载，解析，章节处理，内容处理等问题
 * 引擎增加只需要修改initModels 添加实体类，减少代码建设量
 */
public class WebBookModelControl {
    /**
     * 解析引擎队列
     */
    private List<IReaderBookModel> models = new ArrayList<>();


    private static volatile WebBookModelControl webBookModel;

    private WebBookModelControl() {

    }

    public static WebBookModelControl getInstance() {
        if (webBookModel == null) {
            synchronized (WebBookModelControl.class) {
                if (webBookModel == null) {
                    webBookModel = new WebBookModelControl();
                    webBookModel.initModels();
                }
            }
        }
        return webBookModel;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 增加引擎
     */
    private void initModels() {
        //只具有搜索功能的下发服务器,为用户点击的上报数据

        models.add(ContentSoDuModelImpl.getInstance());



    }



    /**
     * 根据 图书url获取章节目录
     *
     * @return
     */
    public Single<List<BookChapterBean>> getBookChapters(CollBookBean collBookBean) {
//
//        Uri uri = Uri.parse(collBookBean.getBookChapterUrl());
//        String TAG = uri.getScheme() + "://" + uri.getHost();
//        Logger.d("Current website:  " + TAG);

        for (IReaderBookModel model : models) {
//            if (model.getTAG().equals(TAG)) {
               return model.getBookChapters(collBookBean).map(new Function<List<BookChapterBean>, List<BookChapterBean>>() {
                    @Override
                    public List<BookChapterBean> apply(List<BookChapterBean> bookChapterBeans) throws Exception {
                        return removeDuplication(bookChapterBeans);
                    }
                });
//            }
        }
        return null;
    }
    private List<BookChapterBean> removeDuplication( List<BookChapterBean> bookChapterSource){
        CopyOnWriteArrayList<BookChapterBean> result=new CopyOnWriteArrayList<>();
        for (int i = 0; i < bookChapterSource.size(); i++) {

            Iterator<BookChapterBean> iterator = result.iterator();
            while (iterator.hasNext()){
                BookChapterBean bookTemp=iterator.next();
                if (bookTemp.getId().equals(bookChapterSource.get(i).getId())){
                    result.remove(bookTemp);
                }
            }
            result.add(bookChapterSource.get(i));
        }
        for (int i = 0; i < result.size(); i++) {
            result.get(i).setPosition(i);
        }

        return result;
    }


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 获取章节内容
     *
     * @return
     */
    public Single<ChapterInfoBean> getChapterInfo(String bookId,String chapterId) {
//        Uri uri = Uri.parse(url);
//        String TAG = uri.getScheme() + "://" + uri.getHost();
//        Logger.d("Current website" + TAG);

        for (IReaderBookModel model : models) {
//            if (model.getTAG().equals(TAG)) {
                return model.getChapterInfo(bookId,chapterId);
//            }
        }
        return null;

    }


}
